package com.onLineCar.dispatch.rpc.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.dispatch.entity.orderEnum.OrderStatusEnum;
import com.onLineCar.dispatch.exception.ServiceCodeEnum;
import com.onLineCar.dispatch.exception.ServiceException;
import com.onLineCar.dispatch.rpc.MapRPC;
import com.onLineCar.dispatch.rpc.OrderRPC;
import com.onLineCar.dispatch.rpc.VehicleManageRPC;
import com.onLineCar.dispatch.rpc.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Component
@Slf4j
public class RpcServiceImpl {

    @Resource
    private VehicleManageRPC vehicleManageRPC;

    @Resource
    private OrderRPC orderRpc;

    @Resource
    private MapRPC mapRPC;

    public List<ApiVehicleInfoDTO> queryVehicleInfoByVehicleIds(List<Integer> vehicleIds) {
        Result<List<ApiVehicleInfoDTO>> listResult = null;
        try {
            listResult = vehicleManageRPC.queryVehicleInfoByVehicleIds(vehicleIds);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用车辆管理服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e);
        }
        if (CollectionUtil.isEmpty(listResult.getData())) {
            log.error(">>>>>>>>>调用车辆管理服务数据为Null");
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_NULL);
        }
        return listResult.getData();
    }


    public TblOrder queryOrderByVehicleIds(String tblOrder) {
        Result<TblOrder> listResult = null;
        try {
            listResult = orderRpc.selectOrderNumber(tblOrder);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用订单服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e.getMessage());
        }
        if (ObjectUtil.isEmpty(listResult.getData())) {
            log.error(">>>>>>>>>调用订单服务异常为Null");
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_NULL);
        }
        return listResult.getData();
    }

    public String getAddressByLocation(StringBuilder pointStr) {
        Result<AMapAddressData> result = null;
        try {
//            result = mapRPC.getAddressByLocation(pointStr.toString());
        } catch (Exception e) {
            log.error(">>>>>>>>>调用地图服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.MAP_SERVICE_REMOTE_ERROR, e);
        }
//        if (ObjectUtil.isEmpty(result.getData())) {
//            log.error(">>>>>>>>>调用调度服务数据为Null");
//            throw new ServiceException(ServiceCodeEnum.MAP_SERVICE_REMOTE_NULL);
//        }
//        Regeocode regeocode = result.getData().getRegeocode();
//        AddressComponent addressComponent = regeocode.getAddressComponent();
//        log.info("调用地图服务查出的district:{}", addressComponent.getDistrict());
//        return addressComponent.getDistrict();
        return "奉贤区";
    }

    public Integer querySupportCity(String district) {
        Result<Integer> result = null;
        try {
            result = vehicleManageRPC.supportCity(district);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用调度服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e);
        }
        if (ObjectUtil.isEmpty(result.getData())) {
            log.error(">>>>>>>>>调用调度服务异常为empty");
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_NULL);
        }
        return result.getData();
    }


    public Boolean driverService(String district) {
        Result<Boolean> result = null;
        try {
            result = vehicleManageRPC.driverService(district);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用调度服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e);
        }
        if (ObjectUtil.isEmpty(result.getData())) {
            log.error(">>>>>>>>>调用调度服务异常为empty");
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_NULL);
        }
        return result.getData();
    }

    public Boolean havaBillingRule(String address) {
        Result<Boolean> result = null;
        try {
            result = vehicleManageRPC.havaBillingRule(address);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用调度服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e);
        }
        if (ObjectUtil.isEmpty(result.getData())) {
            log.error(">>>>>>>>>调用调度服务异常为empty");
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_NULL);
        }
        return result.getData();
    }

    public Boolean isBlockUser(Long userId) {
        return !userId.equals(NumberUtils.LONG_ONE);
    }

    public Boolean queryOrderByUserId(Long userId) {
        Result<List<TblOrder>> result = null;
        try {
            result = orderRpc.selectAllByUserId(userId);
        } catch (Exception e) {
            log.error(">>>>>>>>>调用订单服务异常 exception:{}", e.getMessage(), e);
            throw new ServiceException(ServiceCodeEnum.VEHICLE_SERVICE_REMOTE_ERROR, e);
        }
        if (ObjectUtil.isEmpty(result.getData())) {
            log.error(">>>>>>>>>调用订单服务数据为Null,此用户没有待支付的订单");
            return false;
        }
        AtomicReference<Integer> count = new AtomicReference<>(0);
        result.getData().forEach(tblOrder -> {
            if (!tblOrder.getStatus().equals(OrderStatusEnum.ORDER_STATUS_CANCEL.getOrderCode()) ||
                   !tblOrder.getStatus().equals(OrderStatusEnum.ORDER_STATUS_PAY_SUCCESS.getOrderCode())) {
                count.set(count.get() + 1);
            }
        });
        if (count.get() >2){
            return true;
        }
        return false;
    }
}
